//
//  LoginManager.m
//  bsl-sdk
//
//  Created by Fanty on 13-12-1.
//
//

#import "LoginManager.h"
#import <MainModuleSDK/DataCenter.h>
#import "ApiConstant+Login.h"
#import <chameleon-ios-sdk/CApplication.h>
#import <MainModuleSDK/MainModule.h>

#import <MainModuleSDK/AsyncTask.h>

#import "ASIFormDataRequest.h"
#import "LoginParser.h"


@interface LoginManager()
@end

@implementation LoginManager{
}


-(void)restoreAccount{
    NSUserDefaults* userDefault=[NSUserDefaults standardUserDefaults];
    
    [DataCenter defaultCenter].username=[userDefault objectForKey:@"loginModule_username"];
    [DataCenter defaultCenter].password=[userDefault objectForKey:@"loginModule_password"];
    [DataCenter defaultCenter].isRemember=([[userDefault objectForKey:@"loginModule_isRemember"] isEqualToString:@"true"]);
    
}

-(void)saveAccount{
    NSUserDefaults* userDefault=[NSUserDefaults standardUserDefaults];
    
    [userDefault setObject:[DataCenter defaultCenter].username forKey:@"loginModule_username"];
    [userDefault setObject:[DataCenter defaultCenter].password forKey:@"loginModule_password"];
    [userDefault setObject:([DataCenter defaultCenter].isRemember?@"true":@"false") forKey:@"loginModule_isRemember"];
    [userDefault synchronize];
}

-(AsyncTask*)login:(NSString*)name password:(NSString*)password{
    MainModule* mainModule=(MainModule*)[[CApplication sharedApplication] moduleForIdentifier:@"Main"];
    
    ASIFormDataRequest *request = [mainModule requestWithURL:[[ApiConstant defaultConstant] cubeLoginURL]];
    request.timeOutSeconds=20.0f;
    [request setPersistentConnectionTimeoutSeconds:20.0f];
    [request setPostValue:name forKey:@"username"];
    [request setPostValue:password forKey:@"password"];

    
    LoginParser* parser=[[LoginParser alloc] init];

    return [mainModule asyncWithOperation:request parser:parser];
    
}

@end
